﻿using System;
using System.Text;

namespace ExFlash.Records
{
    public class StringRecord : ISwfSerializable
    {
        private readonly int _swfVersion;
        
        public String Text
        {
            get;
            private set;
        }

        public override string ToString()
        {
            return Text;
        }

        public StringRecord(int version)
        {
            _swfVersion = version;
        }

        #region ISwfSerialiable Members

        public void SwfDeserialize(BinaryBitReader reader)
        {
            Encoding enc = (_swfVersion >= 6)? Encoding.UTF8 : Encoding.GetEncoding(1252);

            int strLen = 0;

            while (reader.ReadByte() != 0)
                strLen++;

            reader.Position -= (strLen + 1);
            Byte[] arr = new Byte[strLen + 1];
            reader.Read(arr, 0, strLen + 1);
            Text = enc.GetString(arr);
        }

        public void SwfSerialize(byte[] arr, ref int offset)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
